$(function() {
    $("div.menu ul li a").click(function() {
        if (!$(this).parent().hasClass("active")) {
            $("div.menu ul li.active").removeClass("active");
            $(this).parent().addClass("active");
            $("div.content form").hide();
            $("div.content form#"+$(this).attr("id")+"-search").show();
        }
    });
    if ($("#query-text").val() != "") {
        doSearch(lastPage);
    }
    $("#query-text").keypress(function(event) {
        if ( event.which == 13 ) {
            event.preventDefault();
            doSearch(1);
        }
    });
    $("#do-filter-search-apply").click(function() {
        doFiltre(1);
    //alert($("#do-filter-search").serialize());
    });
    $("div.content form").hide();
    var pageId = $("div.menu ul li.active a").attr("id");
    $("div.content form#"+pageId+"-search").show();
    $("div.content").show();
    $("div.menu ul li.active a").show();
    $("#loading").fadeOut();
});

var lastPage = 1;
function doSearch(page) {
    var text = $("#query-text").val();
    $("#query-text").attr("disabled", "disabled");
    $("#loading").fadeIn();
    lastPage = page;
    $.post(base+'/query?text='+text+"&page="+page, {
        "text": text
    }, function(response) {
        $("#loading").fadeOut();
        $("#query-text").removeAttr("disabled");
        if (response.done) {
            var text = '';
            for (i=0; i<response.result.length; i++) {
                text += '<div class="result"><div class="title"><a href="'+base+'/view?q='+$.base64.encode(response.result[i].url)+'">'+response.result[i].title+'</a> - <span class="price">' + response.result[i].proceEuro + ' Euro</span></div><div class="description">'+response.result[i].description+'</div></div>';
            }
            $("#result .output").html(text);
            var pages = '';
            for (i=1; i<=response.pages; i++) {
                pages += '<a id="link-page-' + i + '" href="javascript:doSearch(' + i + ');"' + (lastPage==i?' class="active"':'') + '>' + i + '</a> ';
            }
            $("#result .pages").html(pages);
            if (parseInt(response.time) >= 0) {
                $("#result .time").text(response.items + " in " + response.time + " seconds");
            }
        }
    }, 'json');
}

function doFiltre(page) {
    var query = $("#do-filter-search").serialize();
    $("#do-filter-search").attr("disabled", "disabled");
    $("#loading").fadeIn();
    lastPage = page;
    $.post(base+"/filtre?page="+page, query, function(response) {
        $("#loading").fadeOut();
        $("#do-filter-search").removeAttr("disabled");
        if (response.done) {
            var text = '';
            for (i=0; i<response.result.length; i++) {
                text += '<div class="result"><div class="title"><a href="'+base+'/view?q='+$.base64.encode(response.result[i].url)+'">'+response.result[i].title+'</a> - <span class="price">' + response.result[i].proceEuro + ' Euro</span></div><div class="description">'+response.result[i].description+'</div></div>';
            }
            $("#result .output").html(text);
            var pages = '';
            for (i=1; i<=response.pages; i++) {
                pages += '<a id="link-page-' + i + '" href="javascript:doFiltre(' + i + ');"' + (lastPage==i?' class="active"':'') + '>' + i + '</a> ';
            }
            $("#result .pages").html(pages);
            if (parseInt(response.time) >= 0) {
                $("#result .time").text(response.items + " in " + response.time + " seconds");
            }
        }
    }, 'json');
}
